package com.cn.springcloudwechat.upload;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.controller.base.BaseController;
import com.cn.springcloudwechat.model.cos.CosClientEntity;
import com.cn.springcloudwechat.model.upload.YmUpload;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class UploadUserController extends BaseController {
    private static final String basePath = CosClientEntity.realmName + File.separator;
    // 头像上传
    @RequestMapping("/web/uploadUserHeader")
    public AjaxRes upload( @RequestParam("imageFile") MultipartFile imageFile) {
        YmUpload ymUpload = new YmUpload();
        AjaxRes ajaxRes = getAjaxRes();
        try {
            String imageFilePath = FileUtil.upload(imageFile);
            if (StringUtils.isNotBlank(imageFilePath)) {
                ymUpload.setImagePath(basePath + imageFilePath);
            }
            ajaxRes.setSucceed(ymUpload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ajaxRes;
    }
}
